// pages/setup/setup.js
var handler = require("../../utils/function.js");
const app = getApp(),
  api = require("../../api.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    typeindex: 0,
    type: ["上班", "下班"],
    listenWay: ["抢单模式", "指派模式"],
    listenWayDefault: 0,
    orderHobby: ["全部", "实时", "预约"],
    orderHobbyDefault: 0,
    mileageList: [0, 1, 2, 3, 5],
    progress: [0, 1, 2, 3],
    progressDefault: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取优惠券
    if (options.uid) {
      app.shareCouponsMessage(options.uid)
    }
  },
  // intoPageHandler 页面跳转
  intoPageHandler: function (e) {
    var that = this;
    // 获取公里列表
    let mileageList = this.data.mileageList;
    let scope = mileageList[e.currentTarget.dataset.scope + 1];
    app.request({
      url: api.driver.setUp,
      method: 'post',
      data: {
        work_status: that.data.typeindex,
        way: e.currentTarget.dataset.way,
        like: e.currentTarget.dataset.like,
        scope: scope,
      },
      success: function (rs) {
        if (rs.code == 1) {
          wx.navigateBack({
            delta: 1
          })
          // handler.intoPageHandler(e.currentTarget.dataset.skip)
        } else {
          wx.showToast({
            title: rs.msg,
            duration: 1000,
          })
        }
      }
    })
  },
  typeclink: function (e) {
    this.setData({
      typeindex: e.currentTarget.dataset.check
    });
  },
  checklistenWayHnadler: function (e) {
    console.log('设置听单方式', e.currentTarget.dataset.check);
    this.setData({
      listenWayDefault: e.currentTarget.dataset.check
    });
  },
  checkOrderHobbyHnadler: function (e) {
    this.setData({
      orderHobbyDefault: e.currentTarget.dataset.check
    });
  },
  checkProgressHandler: function (e) {
    this.setData({
      progressDefault: e.currentTarget.dataset.check
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // 获取公里数
    this.getScorp()
    // 获取用户参数

  },
  // 获取接单范围
  getScorp: function () {
    var that = this;
    app.request({
      url: api.scopeseting.index,
      method: 'post',
      data: {},
      success: function (rs) {
        var mileageList = ["0"];
        for (var i = 0; i < rs.data.scope.length; i++) {
          mileageList.push(rs.data.scope[i])
        }
        that.setData({
          mileageList,
          scope: rs.data.scope
        })
        that.getUserSetUp(rs.data.scope)
      }
    })
  },
  // 获取用户参数
  getUserSetUp: function (e) {
    var that = this;
    var mileageList = e;
    // if (mileageList)
    app.request({
      url: api.driver.getset,
      method: 'post',
      data: {},
      success: function (rs) {
        var progressDefault = 0;
        for (var i = 0; i < mileageList.length; i++) {
          if (mileageList[i] == rs.data.scope)
            progressDefault = i
        }
        that.setData({
          listenWayDefault: rs.data.way,
          orderHobbyDefault: rs.data.like,
          progressDefault: progressDefault,
          typeindex: rs.data.work_status
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    // console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})